package com.study.thread.juc.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class CyclicBarrier1 {

    private ReentrantLock lock = new ReentrantLock();
    private Condition trip = lock.newCondition();
    private int count = 0;
    private int editCount = 0;

    public CyclicBarrier1(int count) {
        this.count = count;
        this.editCount = count;
    }

    public void await() {
        lock.lock();
        try {
            editCount--;
            if (editCount == 0) {
                trip.signalAll();
                editCount = count;
            } else {
                trip.await();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

}
